﻿
namespace Caliburn.Micro.Logging
{
    using System;
    using System.Diagnostics;
    using Coproject.ViewModels;

    /// <summary>
    /// Implementation of the ILog and ILogExtended interfaces for logging to a ViewModel Collection
    /// <see cref="Debug"/>.
    /// </summary>
    public class LocalLogger : ILog, ILogExtended
    {
        #region Constants
        private const string ErrorText = "ERROR";
        private const string WarnText = "WARN";
        private const string InfoText = "INFO";
        #endregion

        #region Fields
        private readonly Type _type;
        #endregion

        #region Constructors
        public LocalLogger(Type type)
        {
            _type = type;
        }
        #endregion

        #region Helper Methods
        private string CreateLogMessage(string format, params object[] args)
        {
            return string.Format(format, args);
        }
        #endregion

        #region ILog Members
        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        public void Error(Exception exception)
        {
            var src = String.Format("Caliburn - {0}", _type.Name);
            var msg = String.Format("ErrorText: {0}", exception.ToString());
            LoggingViewModel.LogMethod(src, msg);
        }
        /// <summary>
        /// Logs the message as info.
        /// </summary>
        /// <param name="format">A formatted message.</param><param name="args">Parameters to be injected into the formatted message.</param>
        public void Info(string format, params object[] args)
        {
            var src = String.Format("Caliburn - {0}", _type.Name);
            var msg = String.Format("InfoText: {0}", CreateLogMessage(format, args));
            LoggingViewModel.LogMethod(src, msg);
        }
        /// <summary>
        /// Logs the message as a warning.
        /// </summary>
        /// <param name="format">A formatted message.</param><param name="args">Parameters to be injected into the formatted message.</param>
        public void Warn(string format, params object[] args)
        {
            var src = String.Format("Caliburn - {0}", _type.Name);
            var msg = String.Format("WarnText: {0}", CreateLogMessage(format, args));
            LoggingViewModel.LogMethod(src, msg);
        }
        #endregion

        #region Implementation of ILogExtended
        /// <summary>
        /// Logs the message as error.
        /// </summary>
        /// <param name="format">A formatted message.</param>
        /// <param name="args">Parameters to be injected into the formatted message.</param>
        public void Error(string format, params object[] args)
        {
            var src = String.Format("Caliburn - {0}", _type.Name);
            var msg = String.Format("ErrorText: {0}", CreateLogMessage(format, args));
            LoggingViewModel.LogMethod(src, msg);
        }
        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <param name="format">A formatted message.</param>
        /// <param name="args">Parameters to be injected into the formatted message.</param>
        public void Error(Exception exception, string format, params object[] args)
        {
            var src = String.Format("Caliburn - {0}", _type.Name);
            var msg = String.Format("Exception: {0} - ErrorText: {1}", exception.ToString(), CreateLogMessage(format, args));
            LoggingViewModel.LogMethod(src, msg);
        }
        #endregion
    }
}
